var host = "/" + window.location.pathname.split("/")[1];
var checkRow = null;
var dateHis = null;
var ckDate = null;
var postDate = null;
var postData = null;
init = function(){
	$.ajax({
		url:host +"/getLogin",
		dataType:"json",
		type:"POST",
		data:{},
		cache:false,
		async:false,
		success: function(data){
			if(data == null || data == "null"){
				$("#loginBody").css("display","block");
			}else{
				$("#loginInfo").css("display","block");
				$("#nameInfo").text(data.name);
			}
		}
	})
	var bodyHeight = $(window).height();
	var headHeight = $("#head").height()+$("#btns").height();
	$("#easyui").css('height',bodyHeight-headHeight-20);
	$("#easyBody").datagrid({
		singleSelect:true,
		fit:true,
		fitColumns:true,
		loadingMessage:"正在加载数据，请耐心等候！",
		columns:[[
			{field:'ck',checkbox:true}, 
			{field:'id',title:'ID',width:9},
			{field:'contenttext',title:'内容',width:60},    
			{field:'d_date',title:'日期1',width:15},    
			{field:'d_time',title:'时间',width:9},
			{field:'userid',title:'用户编码',width:9},
			{field:'rzlb',title:'类别',width:9},
			{field:'bmid',title:'编码',width:9},
			{field:'txrq',title:'填写日期',width:24},
			{field:'d_date2',title:'日期2',width:24},
			{field:'projName',title:'部门',width:35},
			{field:'del',title:'删除',width:9,formatter:function(value,rowData,index){
				return "<a href='#' onclick='delDiary(\""+rowData.id+"\")'>删除</a>"
			}}
		]],
		onClickRow:function(rowIndex,rowData){
			$("#contenttext").val(rowData.contenttext);
			$("#d_date").val(rowData.d_date);
			$("#d_time").val(rowData.d_time);
			$("#userid").val(rowData.userid);
			$("#rzlb").val(rowData.rzlb);
			$("#bmid").val(rowData.bmid);
			$("#txrq").val(rowData.txrq);
			$("#d_date2").val(rowData.d_date2);
			$("#projName").val(rowData.projName);
		}
	});
	$("#easyBody").datagrid("loadData",getData());
	
	$("#start").datebox({
		onSelect:function(date){
			dt1 = date;
		}
	})
	
	$("#end").datebox({
		onSelect:function(date){
			var start = dt1.Format("yyyy-MM-dd");
			var end = date.Format("yyyy-MM-dd");
			postDate = [];
			if(dt1<date){
				
				var startTime = getDate(start);
				var endTime = getDate(end);
				while((endTime.getTime()-startTime.getTime())>=0){
					var year = startTime.getFullYear();
					var month = startTime.getMonth().toString().length==1?"0"+startTime.getMonth().toString():startTime.getMonth();
					var day = startTime.getDate().toString().length==1?"0"+startTime.getDate():startTime.getDate();
					var endDate = year+"-"+month+"-"+day;
					var day = new Date(endDate).getDay();
					var isIn = endDate in dateHis;
					if(!isIn && day != '0' && day != '6'){
						postDate.push(endDate);
					}
					startTime.setDate(startTime.getDate()+1);
				}
			}
			if(start == end){
				if(!(start in dateHis)){
					postDate.push(start);
				}
			}
		}
	})//.datebox('setValue',bdjz.setTime());
}

delDiary = function(id){
	if(!confirm("确定删除?")){
		return;
	}
	$.ajax({
		url:host + 	"/dDiary",
		type:"POST",
		dataType:"json",
		data:{id:id},
		cache:false,
		async:false,
		success: function(data){
			if(data.indexOf("SQL语句执行成功" !=-1)){
				alert("删除成功");
				$("#easyBody").datagrid('loadData',getData());
			}else{
				alert("删除失败，请重试！");
			}
		}
	})	
}

login = function(){
	// alert(host);
    // var loginAction = "../../pages/user_login.action";
	//alert(host);
	var name = $("#name").val();
	var pswd = $("#pswd").val();
	$.ajax({
		url: host + "/doLogin",
		type:"POST",
		dataType:"json",
		data:{name:name,pswd:pswd},
		cache:false,
		async:false,
		success: function(data){
			alert(data.log);
			if(data.status == "true"){
				$("#loginBody").css("display","none");
				$("#loginInfo").css("display","block");
				$("#nameInfo").text(data.Info.name);
				$("#easyBody").datagrid("loadData",getData());
			}	
		}
	})
}
logout = function(){
	$.ajax({
		url: host + "/doLogout",
		type:"POST",
		dataType:"text",
		data:{},
		cache:false,
		async:false,
		success: function(data){
			alert(data);
			$("#nameInfo").text("");
			$("#loginInfo").css("display","none");
			$("#loginBody").css("display","block");
			$("#easyBody").datagrid("loadData",[]);
		}
	})
}
insert = function(){
	postData = [];
	if(postDate.length > 0){
		for(var i=0;i<postDate.length;i++){
			var value = "";
			value += "'"+ $("#contenttext").val() +"',";
			value += "'"+ postDate[i] +"',";
			value += "'"+ $("#d_time").val() +"',";
			value += "'"+ $("#userid").val() +"',";
			value += "'"+ $("#rzlb").val() +"',";
			value += "'"+ $("#bmid").val() +"',";
			value += "'"+ postDate[i] +" 00:00:00',";
			value += "'"+ postDate[i] +" 00:00:00',";
			value += "'"+ $("#projName").val() +"',";
			value = value.substring(0,value.length-1);
			postData.push("["+value+"]");
		}	
	}else{
		alert("请选择正确的日期格式!");
		return;
	}
	if(!confirm("确定写入"+postDate+"?")){
		return;
	}
	$.ajax({
		type:"POST",
		url:host + "/isDiary",
		data:{data:JSON2.stringify(postData)},
		dataType:"json",
		cache:false,
		async:false,
		success: function(data){
			alert(data.status);
			if(data.errorText != ""){
				alert("出错日期:"+data.errorText);
			}
			$("#easyBody").datagrid('loadData',getData());
		}
	})
}

getData = function(){
	var result = [];
	dateHis = {};
	ckDate = "";
	$.ajax({
		url:host+"/get",
		type:"POST",
		dataType:"text",
		cache:false,
		async:false,
		success: function(data){
			if(data != ""){
				var data = data.substring(data.indexOf("value")+10,data.indexOf("]]"));
				data = data.replace(/\\\"/g, "");
				var list = data.split("],[");
				
				for(var i=0;i<list.length;i++){
					var rList = list[i].split(",");
					var map = {};
					map.id = rList[0];
					map.contenttext = rList[1];
					map.d_date = rList[2];
					map.d_time = rList[3];
					map.userid = rList[5];
					map.rzlb = rList[6];
					map.bmid = rList[7];
					map.txrq = rList[9];
					map.d_date2 = rList[10];
					map.projName = rList[11];
					dateHis[rList[2]] = 0;
					result.push(map);
					if(i == 0){
						$("#id").val(map.id);
						$("#contenttext").val(map.contenttext);
						$("#d_date").val(map.d_date);
						$("#d_time").val(map.d_time);
						$("#userid").val(map.userid);
						$("#rzlb").val(map.rzlb);
						$("#bmid").val(map.bmid);
						$("#txrq").val(map.txrq);
						$("#d_date2").val(map.d_date2);
						$("#projName").val(map.projName);
					}	
					if(i == list.length-1){
						ckDate = rList[1];
					}	
				}
			}
		}
	})
	return result;
}

Date.prototype.Format = function (fmt) { //author: meizz 
    var o = {
        "M+": this.getMonth() + 1, //月份 
        "d+": this.getDate(), //日 
        "h+": this.getHours(), //小时 
        "m+": this.getMinutes(), //分 
        "s+": this.getSeconds(), //秒 
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
        "S": this.getMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}
function getDate(datestr){
  var temp = datestr.split("-");
  var date = new Date(temp[0],temp[1],temp[2]);
  return date;
}

$(document).ready(function() {
	init();
});